home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / sets.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  12KB  |  459 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import generators
  5.  
  6. try:
  7.     from itertools import ifilter, ifilterfalse
  8. except ImportError:
  9.     
  10.     def ifilter(predicate, iterable):
  11.         if predicate is None:
  12.             
  13.             def predicate(x):
  14.                 return x
  15.  
  16.         
  17.         for x in iterable:
  18.             if predicate(x):
  19.                 yield x
  20.                 continue
  21.         
  22.  
  23.     
  24.     def ifilterfalse(predicate, iterable):
  25.         if predicate is None:
  26.             
  27.             def predicate(x):
  28.                 return x
  29.  
  30.         
  31.         for x in iterable:
  32.             if not predicate(x):
  33.                 yield x
  34.                 continue
  35.         
  36.  
  37.     
  38.     try:
  39.         (True, False)
  40.     except NameError:
  41.         (True, False) = (0 == 0, 0 != 0)
  42.  
  43.  
  44. __all__ = [
  45.     'BaseSet',
  46.     'Set',
  47.     'ImmutableSet']
  48.  
  49. class BaseSet(object):
  50.     __slots__ = [
  51.         '_data']
  52.     
  53.     def __init__(self):
  54.         if self.__class__ is BaseSet:
  55.             raise TypeError, 'BaseSet is an abstract class.  Use Set or ImmutableSet.'
  56.         
  57.  
  58.     
  59.     def __len__(self):
  60.         return len(self._data)
  61.  
  62.     
  63.     def __repr__(self):
  64.         return self._repr()
  65.  
  66.     __str__ = __repr__
  67.     
  68.     def _repr(self, sorted = False):
  69.         elements = self._data.keys()
  70.         if sorted:
  71.             elements.sort()
  72.         
  73.         return '%s(%r)' % (self.__class__.__name__, elements)
  74.  
  75.     
  76.     def __iter__(self):
  77.         return self._data.iterkeys()
  78.  
  79.     
  80.     def __cmp__(self, other):
  81.         raise TypeError, "can't compare sets using cmp()"
  82.  
  83.     
  84.     def __eq__(self, other):
  85.         if isinstance(other, BaseSet):
  86.             return self._data == other._data
  87.         else:
  88.             return False
  89.  
  90.     
  91.     def __ne__(self, other):
  92.         if isinstance(other, BaseSet):
  93.             return self._data != other._data
  94.         else:
  95.             return True
  96.  
  97.     
  98.     def copy(self):
  99.         result = self.__class__()
  100.         result._data.update(self._data)
  101.         return result
  102.  
  103.     __copy__ = copy
  104.     
  105.     def __deepcopy__(self, memo):
  106.         deepcopy = deepcopy
  107.         import copy
  108.         result = self.__class__()
  109.         memo[id(self)] = result
  110.         data = result._data
  111.         value = True
  112.         for elt in self:
  113.             data[deepcopy(elt, memo)] = value
  114.         
  115.         return result
  116.  
  117.     
  118.     def __or__(self, other):
  119.         if not isinstance(other, BaseSet):
  120.             return NotImplemented
  121.         
  122.         return self.union(other)
  123.  
  124.     
  125.     def union(self, other):
  126.         result = self.__class__(self)
  127.         result._update(other)
  128.         return result
  129.  
  130.     
  131.     def __and__(self, other):
  132.         if not isinstance(other, BaseSet):
  133.             return NotImplemented
  134.         
  135.         return self.intersection(other)
  136.  
  137.     
  138.     def intersection(self, other):
  139.         if not isinstance(other, BaseSet):
  140.             other = Set(other)
  141.         
  142.         if len(self) <= len(other):
  143.             little = self
  144.             big = other
  145.         else:
  146.             little = other
  147.             big = self
  148.         common = ifilter(big._data.has_key, little)
  149.         return self.__class__(common)
  150.  
  151.     
  152.     def __xor__(self, other):
  153.         if not isinstance(other, BaseSet):
  154.             return NotImplemented
  155.         
  156.         return self.symmetric_difference(other)
  157.  
  158.     
  159.     def symmetric_difference(self, other):
  160.         result = self.__class__()
  161.         data = result._data
  162.         value = True
  163.         selfdata = self._data
  164.         
  165.         try:
  166.             otherdata = other._data
  167.         except AttributeError:
  168.             otherdata = Set(other)._data
  169.  
  170.         for elt in ifilterfalse(otherdata.has_key, selfdata):
  171.             data[elt] = value
  172.         
  173.         for elt in ifilterfalse(selfdata.has_key, otherdata):
  174.             data[elt] = value
  175.         
  176.         return result
  177.  
  178.     
  179.     def __sub__(self, other):
  180.         if not isinstance(other, BaseSet):
  181.             return NotImplemented
  182.         
  183.         return self.difference(other)
  184.  
  185.     
  186.     def difference(self, other):
  187.         result = self.__class__()
  188.         data = result._data
  189.         
  190.         try:
  191.             otherdata = other._data
  192.         except AttributeError:
  193.             otherdata = Set(other)._data
  194.  
  195.         value = True
  196.         for elt in ifilterfalse(otherdata.has_key, self):
  197.             data[elt] = value
  198.         
  199.         return result
  200.  
  201.     
  202.     def __contains__(self, element):
  203.         
  204.         try:
  205.             return element in self._data
  206.         except TypeError:
  207.             transform = getattr(element, '__as_temporarily_immutable__', None)
  208.             if transform is None:
  209.                 raise 
  210.             
  211.             return transform() in self._data
  212.  
  213.  
  214.     
  215.     def issubset(self, other):
  216.         self._binary_sanity_check(other)
  217.         if len(self) > len(other):
  218.             return False
  219.         
  220.         for elt in ifilterfalse(other._data.has_key, self):
  221.             return False
  222.         
  223.         return True
  224.  
  225.     
  226.     def issuperset(self, other):
  227.         self._binary_sanity_check(other)
  228.         if len(self) < len(other):
  229.             return False
  230.         
  231.         for elt in ifilterfalse(self._data.has_key, other):
  232.             return False
  233.         
  234.         return True
  235.  
  236.     __le__ = issubset
  237.     __ge__ = issuperset
  238.     
  239.     def __lt__(self, other):
  240.         self._binary_sanity_check(other)
  241.         if len(self) < len(other):
  242.             pass
  243.         return self.issubset(other)
  244.  
  245.     
  246.     def __gt__(self, other):
  247.         self._binary_sanity_check(other)
  248.         if len(self) > len(other):
  249.             pass
  250.         return self.issuperset(other)
  251.  
  252.     
  253.     def _binary_sanity_check(self, other):
  254.         if not isinstance(other, BaseSet):
  255.             raise TypeError, 'Binary operation only permitted between sets'
  256.         
  257.  
  258.     
  259.     def _compute_hash(self):
  260.         result = 0
  261.         for elt in self:
  262.             result ^= hash(elt)
  263.         
  264.         return result
  265.  
  266.     
  267.     def _update(self, iterable):
  268.         data = self._data
  269.         if isinstance(iterable, BaseSet):
  270.             data.update(iterable._data)
  271.             return None
  272.         
  273.         value = True
  274.  
  275.  
  276.  
  277. class ImmutableSet(BaseSet):
  278.     __slots__ = [
  279.         '_hashcode']
  280.     
  281.     def __init__(self, iterable = None):
  282.         self._hashcode = None
  283.         self._data = { }
  284.         if iterable is not None:
  285.             self._update(iterable)
  286.         
  287.  
  288.     
  289.     def __hash__(self):
  290.         if self._hashcode is None:
  291.             self._hashcode = self._compute_hash()
  292.         
  293.         return self._hashcode
  294.  
  295.     
  296.     def __getstate__(self):
  297.         return (self._data, self._hashcode)
  298.  
  299.     
  300.     def __setstate__(self, state):
  301.         (self._data, self._hashcode) = state
  302.  
  303.  
  304.  
  305. class Set(BaseSet):
  306.     __slots__ = []
  307.     
  308.     def __init__(self, iterable = None):
  309.         self._data = { }
  310.         if iterable is not None:
  311.             self._update(iterable)
  312.         
  313.  
  314.     
  315.     def __getstate__(self):
  316.         return (self._data,)
  317.  
  318.     
  319.     def __setstate__(self, data):
  320.         (self._data,) = data
  321.  
  322.     
  323.     def __hash__(self):
  324.         raise TypeError, "Can't hash a Set, only an ImmutableSet."
  325.  
  326.     
  327.     def __ior__(self, other):
  328.         self._binary_sanity_check(other)
  329.         self._data.update(other._data)
  330.         return self
  331.  
  332.     
  333.     def union_update(self, other):
  334.         self._update(other)
  335.  
  336.     
  337.     def __iand__(self, other):
  338.         self._binary_sanity_check(other)
  339.         self._data = (self & other)._data
  340.         return self
  341.  
  342.     
  343.     def intersection_update(self, other):
  344.         if isinstance(other, BaseSet):
  345.             self &= other
  346.         else:
  347.             self._data = self.intersection(other)._data
  348.  
  349.     
  350.     def __ixor__(self, other):
  351.         self._binary_sanity_check(other)
  352.         self.symmetric_difference_update(other)
  353.         return self
  354.  
  355.     
  356.     def symmetric_difference_update(self, other):
  357.         data = self._data
  358.         value = True
  359.         if not isinstance(other, BaseSet):
  360.             other = Set(other)
  361.         
  362.         if self is other:
  363.             self.clear()
  364.         
  365.         for elt in other:
  366.             if elt in data:
  367.                 del data[elt]
  368.                 continue
  369.             data[elt] = value
  370.         
  371.  
  372.     
  373.     def __isub__(self, other):
  374.         self._binary_sanity_check(other)
  375.         self.difference_update(other)
  376.         return self
  377.  
  378.     
  379.     def difference_update(self, other):
  380.         data = self._data
  381.         if not isinstance(other, BaseSet):
  382.             other = Set(other)
  383.         
  384.         if self is other:
  385.             self.clear()
  386.         
  387.         for elt in ifilter(data.has_key, other):
  388.             del data[elt]
  389.         
  390.  
  391.     
  392.     def update(self, iterable):
  393.         self._update(iterable)
  394.  
  395.     
  396.     def clear(self):
  397.         self._data.clear()
  398.  
  399.     
  400.     def add(self, element):
  401.         
  402.         try:
  403.             self._data[element] = True
  404.         except TypeError:
  405.             transform = getattr(element, '__as_immutable__', None)
  406.             if transform is None:
  407.                 raise 
  408.             
  409.             self._data[transform()] = True
  410.  
  411.  
  412.     
  413.     def remove(self, element):
  414.         
  415.         try:
  416.             del self._data[element]
  417.         except TypeError:
  418.             transform = getattr(element, '__as_temporarily_immutable__', None)
  419.             if transform is None:
  420.                 raise 
  421.             
  422.             del self._data[transform()]
  423.  
  424.  
  425.     
  426.     def discard(self, element):
  427.         
  428.         try:
  429.             self.remove(element)
  430.         except KeyError:
  431.             pass
  432.  
  433.  
  434.     
  435.     def pop(self):
  436.         return self._data.popitem()[0]
  437.  
  438.     
  439.     def __as_immutable__(self):
  440.         return ImmutableSet(self)
  441.  
  442.     
  443.     def __as_temporarily_immutable__(self):
  444.         return _TemporarilyImmutableSet(self)
  445.  
  446.  
  447.  
  448. class _TemporarilyImmutableSet(BaseSet):
  449.     
  450.     def __init__(self, set):
  451.         self._set = set
  452.         self._data = set._data
  453.  
  454.     
  455.     def __hash__(self):
  456.         return self._set._compute_hash()
  457.  
  458.  
  459.